package leetcode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q1209.java
 * @Author : WangRuoyu
 * @Date 2023/11/9 17:27
 */

// TODO
public class Q1209 {
    public boolean check(StringBuilder stack, int k) {
        for (int i = 1, j = stack.length() - 2; i < k; i++, j--) {
            if (j < 0 || stack.charAt(j) != stack.charAt(stack.length() - 1)) return false;
        }
        return true;
    }

    public String removeDuplicates(String s, int k) {
        StringBuilder stack = new StringBuilder(s.length());
        for (int i = 0; i < s.length(); i++) {
            stack.append(s.charAt(i));
            if (check(stack, k)) {
                stack.delete(stack.length() - k, stack.length());
            }
        }
        return stack.toString();
    }

    public static void main(String[] args) {
        Q1209 sl = new Q1209();
        String s = "deeedbbcccbdaa";
        int k = 3;
        System.out.println(sl.removeDuplicates(s, k));
    }
}